#ifndef COMPRESS_PROBE
#define COMPRESS_PROBE

#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"

TEXTURE2D(_MeanTex);
SAMPLER(sampler_MeanTex);

TEXTURE2D(_AxesTex);
SAMPLER(sampler_AxesTex);

TEXTURE2D(_CoeffTex);
SAMPLER(sampler_CoeffTex);

float _ProbeIndex;
int _ComponentCount;


// #define CALC_SH(sh, j) { \
// int base = (j) * 12; \
// float3 acc = float3(mean[0 + (j)], mean[9 + (j)], mean[18 + (j)]); \
// float3 a0 = axes[base + 0] * c0.x + axes[base + 1] * c0.y + axes[base + 2] * c0.z + axes[base + 3] * c0.w; \
// float3 a1 = axes[base + 4] * c1.x + axes[base + 5] * c1.y + axes[base + 6] * c1.z + axes[base + 7] * c1.w; \
// float3 a2 = axes[base + 8] * c2.x + axes[base + 9] * c2.y + axes[base +10] * c2.z + axes[base +11] * c2.w; \
// sh[j] = float4(acc + a0 + a1 + a2, 0); \
// }


// #define CALC_SH(sh, j) { \
//     int base = (j) * 12; \
//     float3 acc = float3(mean[0 + (j)], mean[9 + (j)], mean[18 + (j)]); \
//     float3 a0 = float3( \
//         dot(c0, float4(axes[base + 0].x, axes[base + 1].x, axes[base + 2].x, axes[base + 3].x)), \
//         dot(c0, float4(axes[base + 0].y, axes[base + 1].y, axes[base + 2].y, axes[base + 3].y)), \
//         dot(c0, float4(axes[base + 0].z, axes[base + 1].z, axes[base + 2].z, axes[base + 3].z)) \
//     ); \
//     float3 a1 = float3( \
//         dot(c1, float4(axes[base + 4].x, axes[base + 5].x, axes[base + 6].x, axes[base + 7].x)), \
//         dot(c1, float4(axes[base + 4].y, axes[base + 5].y, axes[base + 6].y, axes[base + 7].y)), \
//         dot(c1, float4(axes[base + 4].z, axes[base + 5].z, axes[base + 6].z, axes[base + 7].z)) \
//     ); \
//     float3 a2 = float3( \
//         dot(c2, float4(axes[base + 8].x, axes[base + 9].x, axes[base + 10].x, axes[base + 11].x)), \
//         dot(c2, float4(axes[base + 8].y, axes[base + 9].y, axes[base + 10].y, axes[base + 11].y)), \
//         dot(c2, float4(axes[base + 8].z, axes[base + 9].z, axes[base + 10].z, axes[base + 11].z)) \
//     ); \
//     sh[j] = float4(acc + a0 + a1 + a2, 0); \
// }

// #define CALC_SH(sh, j) { \
//     int base = (j) * 12; \
//     float3 acc = float3(mean[0 + (j)], mean[9 + (j)], mean[18 + (j)]); \
//     float3 a0 = float3(\
//         dot(coeffs_0_3, float4(axes_x[base + 0], axes_x[base + 1], axes_x[base + 2], axes_x[base + 3])), \
//         dot(coeffs_0_3, float4(axes_y[base + 0], axes_y[base + 1], axes_y[base + 2], axes_y[base + 3])), \
//         dot(coeffs_0_3, float4(axes_z[base + 0], axes_z[base + 1], axes_z[base + 2], axes_z[base + 3])) \
//     ); \
//     float3 a1 = float3(\
//         dot(coeffs_4_7, float4(axes_x[base + 4], axes_x[base + 5], axes_x[base + 6], axes_x[base + 7])), \
//         dot(coeffs_4_7, float4(axes_y[base + 4], axes_y[base + 5], axes_y[base + 6], axes_y[base + 7])), \
//         dot(coeffs_4_7, float4(axes_z[base + 4], axes_z[base + 5], axes_z[base + 6], axes_z[base + 7])) \
//     ); \
//     float3 a2 = float3(\
//         dot(coeffs_8_11, float4(axes_x[base + 8], axes_x[base + 9], axes_x[base + 10], axes_x[base + 11])), \
//         dot(coeffs_8_11, float4(axes_y[base + 8], axes_y[base + 9], axes_y[base + 10], axes_y[base + 11])), \
//         dot(coeffs_8_11, float4(axes_z[base + 8], axes_z[base + 9], axes_z[base + 10], axes_z[base + 11])) \
//     ); \
//     sh[j] = float4(acc + a0 + a1 + a2, 0); \
// }

#define CALC_SH(sh, c) { \
    int base = (c) * 12; \
    float3 acc = float3(mean[0 + c], mean[9 + c], mean[18 + c]); \
    float3 a0 = float3(\
        dot(coeffs_0_3, axes_x4[c * 3]), \
        dot(coeffs_0_3, axes_y4[c * 3]), \
        dot(coeffs_0_3, axes_z4[c * 3]) \
    ); \
    float3 a1 = float3(\
        dot(coeffs_4_7, axes_x4[c * 3 + 1]), \
        dot(coeffs_4_7, axes_y4[c * 3 + 1]), \
        dot(coeffs_4_7, axes_z4[c * 3 + 1]) \
    ); \
    float3 a2 = float3(\
        dot(coeffs_8_11, axes_x4[c * 3 + 2]), \
        dot(coeffs_8_11, axes_y4[c * 3 + 2]), \
        dot(coeffs_8_11, axes_z4[c * 3 + 2]) \
    ); \
    sh[c] = float3(acc + a0 + a1 + a2); \
}



float3 EvaluateSHFull(in float4 simdCoeffs[7], float3 dir)
{
    float4 dir4 = float4(dir, 1.0);
    
    // L0 + L1 项（常数项 + 一阶线性项）
    float3 linearAndConstTerm = float3(
        dot(simdCoeffs[0], dir4),
        dot(simdCoeffs[1], dir4),
        dot(simdCoeffs[2], dir4)
    );

    // L2 第一部分：xy, yz, zz, zx
    float4 vB = float4(
        dir.x * dir.y,
        dir.y * dir.z,
        dir.z * dir.z,
        dir.z * dir.x
    );

    float3 quadratic1 = float3(
        dot(simdCoeffs[3], vB),
        dot(simdCoeffs[4], vB),
        dot(simdCoeffs[5], vB)
    );

    // L2 第二部分：x² - y²
    float vC = dir.x * dir.x - dir.y * dir.y;

    float3 quadratic2 = float3(
        simdCoeffs[6].x * vC,
        simdCoeffs[6].y * vC,
        simdCoeffs[6].z * vC
    );

    // 汇总
    return linearAndConstTerm + quadratic1 + quadratic2;
}

void PackSH9ToSimdCoeffs(float3 sh[9], out float4 simdCoeffs[7])
{
    // L0 (常数项) + L1（线性项）-> 3个float4
    simdCoeffs[0] = float4(sh[0].r, sh[1].r, sh[2].r, sh[3].r); // Red 通道
    simdCoeffs[1] = float4(sh[0].g, sh[1].g, sh[2].g, sh[3].g); // Green 通道
    simdCoeffs[2] = float4(sh[0].b, sh[1].b, sh[2].b, sh[3].b); // Blue 通道

    // L2 第一部分（xy, yz, zz, zx）-> 3个float4
    simdCoeffs[3] = float4(sh[4].r, sh[5].r, sh[6].r, sh[7].r); // Red
    simdCoeffs[4] = float4(sh[4].g, sh[5].g, sh[6].g, sh[7].g); // Green
    simdCoeffs[5] = float4(sh[4].b, sh[5].b, sh[6].b, sh[7].b); // Blue

    // L2 第二部分（x² - y²）-> float4（最后一项只用到 xyz）
    simdCoeffs[6] = float4(sh[8].r, sh[8].g, sh[8].b, 0.0);
}

void GetShaderConstantsFromNormalizedSH(float3 sh[9], out float4 outCoefficients[7])
{
    // R/G/B 分通道计算
    [unroll]
    for (int iC = 0; iC < 3; iC++)
    {
        // L1 (bands 1,2,3) + (L0 - L2.2)
        float x = sh[3].x; // L1.y
        float y = sh[1].x; // L1.x
        float z = sh[2].x; // L1.z
        float w = sh[0].x - sh[6].x; // L0 - L2.2

        if (iC == 1) { x = sh[3].y; y = sh[1].y; z = sh[2].y; w = sh[0].y - sh[6].y; }
        else if (iC == 2) { x = sh[3].z; y = sh[1].z; z = sh[2].z; w = sh[0].z - sh[6].z; }

        outCoefficients[iC] = float4(x, y, z, w);
    }

    // L2 (bands 4,5,6,7)
    [unroll]
    for (int iC = 0; iC < 3; iC++)
    {
        float x = sh[4].x;
        float y = sh[5].x;
        float z = sh[6].x * 3.0f;
        float w = sh[7].x;

        if (iC == 1) { x = sh[4].y; y = sh[5].y; z = sh[6].y * 3.0f; w = sh[7].y; }
        else if (iC == 2) { x = sh[4].z; y = sh[5].z; z = sh[6].z * 3.0f; w = sh[7].z; }

        outCoefficients[iC + 3] = float4(x, y, z, w);
    }

    // L2.4（band 8）跨 RGB 放入最后一个 float4，W 分量设为 1.0
    float r = sh[8].x;
    float g = sh[8].y;
    float b = sh[8].z;
    outCoefficients[6] = float4(r, g, b, 1.0f);
    // // R/G/B -> 0/1/2
    // for (int iC = 0; iC < 3; iC++)
    // {
    //     // Swizzle the coefficients to be in { x, y, z, DC } order.
    //     float x = sh[iC, 3]; // L1.y
    //     float y = sh[iC, 1]; // L1.x
    //     float z = sh[iC, 2]; // L1.z
    //     float w = sh[iC, 0] - sh[iC, 6]; // L0 - L2.2
    //
    //     outCoefficients[iC] = float4(x, y, z, w);
    // }
    //
    // for (int iC = 0; iC < 3; iC++)
    // {
    //     float x = sh[iC, 4];        // L2.0
    //     float y = sh[iC, 5];        // L2.1
    //     float z = sh[iC, 6] * 3.0f; // L2.2 * 3
    //     float w = sh[iC, 7];        // L2.3
    //
    //     outCoefficients[iC + 3] = float4(x, y, z, w);
    // }
    //
    // // Final L2.4 term (coefficient 8) across R, G, B
    // float r = sh[0, 8]; // red
    // float g = sh[1, 8]; // green
    // float b = sh[2, 8]; // blue
    //
    // outCoefficients[6] = float4(r, g, b, 1.0f); // last slot
}

float3 EvalSH9(float3 dir, float3 sh[9])
{
    float shBasis[9] = {
        0.282095,
        0.488603 * dir.y,
        0.488603 * dir.z,
        0.488603 * dir.x,
        1.092548 * dir.x * dir.y,
        1.092548 * dir.y * dir.z,
        0.315392 * (3.0 * dir.z * dir.z - 1.0),
        1.092548 * dir.x * dir.z,
        0.546274 * (dir.x * dir.x - dir.y * dir.y)
    };

    float3 color = float3(0, 0, 0);
    for (int i = 0; i < 9; i++) color += sh[i] * shBasis[i];
    return color;
}

// 用于将 j 映射到 sh 索引
static const int shIndexLUT[9] = {
    0, // j=0 -> sh[0]
    1, // j=1 -> sh[1]
    2, // j=2 -> sh[2]
    0, // j=3 -> sh[0]
    3, // j=4 -> sh[3]
    4, // j=5 -> sh[4]
    5, // j=6 -> sh[5]
    0, // j=7 -> sh[0]
    0  // j=8 -> sh[0]
};

// 用于将 j 映射到通道索引（r/g/b/w -> 0/1/2/3）
static const int channelLUT[9] = {
    0, // j=0 -> r/g/b
    0,
    0,
    3, // L1.y
    0,
    0,
    0,
    2, // L1.z
    1  // L1.x
};

float4 WriteFloat4Component(float4 v, int channel, float value)
{
    return float4(
        channel == 0 ? value : v.x,
        channel == 1 ? value : v.y,
        channel == 2 ? value : v.z,
        channel == 3 ? value : v.w
    );
}

float3 DecodeSH(int probeIndex, float componentCount, float3 sampleDir)
{
    int probeIdx = (int)probeIndex;
    int compCount = (int)componentCount / 4;
    //float3 sh[9];

    // --- Load mean: RGB texture, shape (9 x 3)
    float mean[27];
    [unroll]
    for (int j = 0; j < 27; j++)
    {
        // Texture coords: 9x3 layout
        float2 uv = float2(0.5, (j + 0.5) / 27); // R
        mean[j] = SAMPLE_TEXTURE2D(_MeanTex, sampler_MeanTex, uv);
    }

    /*
    // float b = mean[9];
    // return float3(b, b, b) * 1000;
    
    // if (probeIndex < 27)
    // {
    //     float b = mean[probeIndex];
    //     return float3(b,b,b) * 100;   
    // }
    //
    // return float3(1,0,0);
    */
    

    /*
    //return float3(coeffs[0], coeffs[1], coeffs[2]);
    
    // --- Apply PCA axes
    //[unroll]
    // for (int j = 0; j < 9; j++)
    // {
    //     float3 acc = mean[j]; // Start with mean
    //     [unroll]
    //     for (int c = 0; c < compCount; c++)
    //     {
    //         // Axes UV: 27 x N layout
    //         float2 uv = float2((j * 3 + 0.5) / 27.0, (c + 0.5) / componentCount);
    //         float r = SAMPLE_TEXTURE2D(_AxesTex, sampler_AxesTex, uv).r;
    //
    //         uv.x = (j * 3 + 1.5) / 27.0;
    //         float g = SAMPLE_TEXTURE2D(_AxesTex, sampler_AxesTex, uv).r;
    //
    //         uv.x = (j * 3 + 2.5) / 27.0;
    //         float b = SAMPLE_TEXTURE2D(_AxesTex, sampler_AxesTex, uv).r;
    //
    //         acc += coeffs[c] * float3(r, g, b);
    //     }
    //
    //     sh[j] = acc;
    // }

    // [unroll]
    // for (int rgb = 0; rgb < 3; rgb++)
    // {
    //     [unroll]
    //     for (int j = 0; j < 9; j++)
    //     {
    //         float sum = 0;
    //         [unroll]
    //         for (int c = 0; c < 12; c++)
    //         {
    //             float2 uv = float2((c + 0.5) / 12, (rgb * 9 + j + 0.5) / 27);
    //             float r = SAMPLE_TEXTURE2D(_AxesTex, sampler_AxesTex, uv).r;
    //             sum += coeffs[c] * r;
    //         }
    //
    //         if (rgb == 0)
    //         {
    //             sh[j].r = sum + mean[rgb * 9 + j];
    //         }
    //         else if (rgb == 1)
    //         {
    //             sh[j].g = sum + mean[rgb * 9 + j];
    //         }
    //         else if (rgb == 2)
    //         {
    //             sh[j].b = sum + mean[rgb * 9 + j];
    //         }
    //     }
    // }
    //   
    //
    // float4 sh4[7];
    // GetShaderConstantsFromNormalizedSH(sh, sh4);
    //
    // return EvaluateSHFull(sh4, sampleDir);
*/

    // float3 axes[108] = (float3[108])0;; // 12 * 9

    float axes_x[108] = (float[108])0;
    float axes_y[108] = (float[108])0;
    float axes_z[108] = (float[108])0;
    
    // [unroll]
    // for (int i = 0; i < 9; ++i)
    // {
    //     [unroll]
    //     for (int j = 0; j < 12; ++j)
    //     {
    //         float2 uv = float2((j + 0.5) / 12.0, (i + 0.5 + 0) / 27.0); // i = rgb * 9 + j
    //         float r = SAMPLE_TEXTURE2D(_AxesTex, sampler_AxesTex, uv).r;
    //         axes[i * 12 + j].x = r;
    //
    //         uv = float2((j + 0.5) / 12.0, (i + 0.5 + 9) / 27.0); // i = rgb * 9 + j
    //         r = SAMPLE_TEXTURE2D(_AxesTex, sampler_AxesTex, uv).r;
    //         axes[i * 12 + j].y = r;
    //
    //         uv = float2((j + 0.5) / 12.0, (i + 0.5 + 18) / 27.0); // i = rgb * 9 + j
    //         r = SAMPLE_TEXTURE2D(_AxesTex, sampler_AxesTex, uv).r;
    //         axes[i * 12 + j].z = r;
    //     }
    // }

    [unroll]
    for (int i = 0; i < 9; ++i)
    {
        [unroll]
        for (int j = 0; j < 12; ++j)
        {
            float2 uv = float2((j + 0.5) / 12.0, (i + 0.5 + 0) / 27.0); // i = rgb * 9 + j
            float r = SAMPLE_TEXTURE2D(_AxesTex, sampler_AxesTex, uv).r;
            axes_x[i * 12 + j] = r;

            uv = float2((j + 0.5) / 12.0, (i + 0.5 + 9) / 27.0); // i = rgb * 9 + j
            r = SAMPLE_TEXTURE2D(_AxesTex, sampler_AxesTex, uv).r;
            axes_y[i * 12 + j] = r;

            uv = float2((j + 0.5) / 12.0, (i + 0.5 + 18) / 27.0); // i = rgb * 9 + j
            r = SAMPLE_TEXTURE2D(_AxesTex, sampler_AxesTex, uv).r;
            axes_z[i * 12 + j] = r;
        }
    }

    float4 axes_x4[27] = (float4[27])0;
    float4 axes_y4[27] = (float4[27])0;
    float4 axes_z4[27] = (float4[27])0;

    for (int i = 0; i < 27; ++i)
    {
        axes_x4[i].x = axes_x[i * 4 + 0];
        axes_x4[i].y = axes_x[i * 4 + 1];
        axes_x4[i].z = axes_x[i * 4 + 2];
        axes_x4[i].w = axes_x[i * 4 + 3];

        axes_y4[i].x = axes_y[i * 4 + 0];
        axes_y4[i].y = axes_y[i * 4 + 1];
        axes_y4[i].z = axes_y[i * 4 + 2];
        axes_y4[i].w = axes_y[i * 4 + 3];

        axes_z4[i].x = axes_z[i * 4 + 0];
        axes_z4[i].y = axes_z[i * 4 + 1];
        axes_z4[i].z = axes_z[i * 4 + 2];
        axes_z4[i].w = axes_z[i * 4 + 3];
    }
    
    
    // --- Load coefficients from R only
    // float coeffs[12]; // Max 12 components
    //
    // [unroll]
    // for (int c = 0; c < compCount; c++)
    // {
    //     float2 uv = float2((probeIdx + 0.5) / 1000, (c + 0.5) / 12);
    //     coeffs[c] = SAMPLE_TEXTURE2D(_CoeffTex, sampler_CoeffTex, uv).r;
    // }

    // float4 coeffs_0_3 = float4(coeffs[0], coeffs[1], coeffs[2], coeffs[3]);
    // float4 coeffs_4_7 = float4(coeffs[4], coeffs[5], coeffs[6], coeffs[7]);
    // float4 coeffs_8_11 = float4(coeffs[8], coeffs[9], coeffs[10], coeffs[11]);
    
    // float4 coeffs[3]; // Max 12 components
    //
    // [unroll]
    // for (int c = 0; c < compCount; c++)
    // {
    //     float2 uv = float2((probeIdx + 0.5) / 1000, (c + 0.5) / compCount);
    //     coeffs[c] = SAMPLE_TEXTURE2D(_CoeffTex, sampler_CoeffTex, uv);
    // }

    float2 uv = float2((probeIdx + 0.5) / 1000, (0 + 0.5) / compCount);
    float4 coeffs_0_3 = SAMPLE_TEXTURE2D(_CoeffTex, sampler_CoeffTex, uv);

    uv = float2((probeIdx + 0.5) / 1000, (1 + 0.5) / compCount);
    float4 coeffs_4_7 = SAMPLE_TEXTURE2D(_CoeffTex, sampler_CoeffTex, uv);

    uv = float2((probeIdx + 0.5) / 1000, (2 + 0.5) / compCount);
    float4 coeffs_8_11 = SAMPLE_TEXTURE2D(_CoeffTex, sampler_CoeffTex, uv);
    
    float3 sh[9] = (float3[9])0;

    CALC_SH(sh, 0);
    CALC_SH(sh, 1);
    CALC_SH(sh, 2);
    CALC_SH(sh, 3);
    CALC_SH(sh, 4);
    CALC_SH(sh, 5);
    CALC_SH(sh, 6);
    CALC_SH(sh, 7);
    CALC_SH(sh, 8);
    
    // [unroll]
    // for (int j = 0; j < 9; ++j)
    // {
    //     // sh[j].x = mean[0 + j];
    //     // sh[j].y = mean[9 + j];
    //     // sh[j].z = mean[18 + j];
    //     //
    //     // [unroll]
    //     // for (int i = 0; i < 12; i++)
    //     // {
    //     //     sh[j] += coeffs[i] * axes[j * 12 + i];
    //     // }
    //
    //     int base = j * 12;
    //     float3 acc = float3(mean[0 + j], mean[9 + j], mean[18 + j]);
    //
    //     float4 c0 = float4(coeffs[0], coeffs[1], coeffs[2], coeffs[3]);
    //     float4 c1 = float4(coeffs[4], coeffs[5], coeffs[6], coeffs[7]);
    //     float4 c2 = float4(coeffs[8], coeffs[9], coeffs[10], coeffs[11]);
    //
    //     float3 a0 = axes[base + 0] * c0.x + axes[base + 1] * c0.y +
    //                 axes[base + 2] * c0.z + axes[base + 3] * c0.w;
    //     float3 a1 = axes[base + 4] * c1.x + axes[base + 5] * c1.y +
    //                 axes[base + 6] * c1.z + axes[base + 7] * c1.w;
    //     float3 a2 = axes[base + 8] * c2.x + axes[base + 9] * c2.y +
    //                 axes[base +10] * c2.z + axes[base +11] * c2.w;
    //
    //     sh[j] = float4(acc + a0 + a1 + a2, 0);
    // }

    float4 sh4[7];
    GetShaderConstantsFromNormalizedSH(sh, sh4);
    
    return EvaluateSHFull(sh4, sampleDir);;
}

float3 DecodePCA(float probeIndex, float3 sampleDir)
{
    // --- Load Mean SH (27 floats = 9 texels of RGB) ---
    float3 mean[9];
    for (int m = 0; m < 9; m++)
    {
        mean[m] = SAMPLE_TEXTURE2D(_MeanTex, sampler_MeanTex, float2(0, m)).rgb;
    }

    // --- Load Coefficients ---
    float coeffs[12];
    for (int k = 0; k < 12; k++)
    {
        float4 c = SAMPLE_TEXTURE2D(_CoeffTex, sampler_CoeffTex, float2(probeIndex, k));
        coeffs[k] = c.r; // You can expand to RGBA if needed
    }

    // --- Apply PCA Axes × Coeffs ---
    float3 decoded[9];
    for (int d = 0; d < 9; d++) decoded[d] = mean[d]; // <- USE MEAN

    UNITY_UNROLL
    for (int k = 0; k < _ComponentCount; k++)
    {
        // for (int j = 0; j < 9; j++) // 3 x 9 = 27
        // {
        //     float2 uv = float2(k, j);
        //     float3 axis = SAMPLE_TEXTURE2D(_AxesTex, sampler_AxesTex, uv).rgb;
        //     
        //     decoded[j] += coeffs[k] * axis;
        // }

        // [unroll]
        // for (int j = 0; j <= 27; j++) // 3 x 9 = 27
        // {
        //     decoded[j] += coeffs[k] * SAMPLE_TEXTURE2D(_AxesTex, sampler_AxesTex, float2(k, j)).r;
        // }

        decoded[0].r += coeffs[k] * SAMPLE_TEXTURE2D(_AxesTex, sampler_AxesTex, float2(k, 0)).r;
        decoded[0].g += coeffs[k] * SAMPLE_TEXTURE2D(_AxesTex, sampler_AxesTex, float2(k, 1)).r;
        decoded[0].b += coeffs[k] * SAMPLE_TEXTURE2D(_AxesTex, sampler_AxesTex, float2(k, 2)).r;

        decoded[1].r += coeffs[k] * SAMPLE_TEXTURE2D(_AxesTex, sampler_AxesTex, float2(k, 3)).r;
        decoded[1].g += coeffs[k] * SAMPLE_TEXTURE2D(_AxesTex, sampler_AxesTex, float2(k, 4)).r;
        decoded[1].b += coeffs[k] * SAMPLE_TEXTURE2D(_AxesTex, sampler_AxesTex, float2(k, 5)).r;

        decoded[2].r += coeffs[k] * SAMPLE_TEXTURE2D(_AxesTex, sampler_AxesTex, float2(k, 6)).r;
        decoded[2].g += coeffs[k] * SAMPLE_TEXTURE2D(_AxesTex, sampler_AxesTex, float2(k, 7)).r;
        decoded[2].b += coeffs[k] * SAMPLE_TEXTURE2D(_AxesTex, sampler_AxesTex, float2(k, 8)).r;

        decoded[3].r += coeffs[k] * SAMPLE_TEXTURE2D(_AxesTex, sampler_AxesTex, float2(k, 9)).r;
        decoded[3].g += coeffs[k] * SAMPLE_TEXTURE2D(_AxesTex, sampler_AxesTex, float2(k, 10)).r;
        decoded[3].b += coeffs[k] * SAMPLE_TEXTURE2D(_AxesTex, sampler_AxesTex, float2(k, 11)).r;

        decoded[4].r += coeffs[k] * SAMPLE_TEXTURE2D(_AxesTex, sampler_AxesTex, float2(k, 12)).r;
        decoded[4].g += coeffs[k] * SAMPLE_TEXTURE2D(_AxesTex, sampler_AxesTex, float2(k, 13)).r;
        decoded[5].b += coeffs[k] * SAMPLE_TEXTURE2D(_AxesTex, sampler_AxesTex, float2(k, 14)).r;

        decoded[5].r += coeffs[k] * SAMPLE_TEXTURE2D(_AxesTex, sampler_AxesTex, float2(k, 15)).r;
        decoded[5].g += coeffs[k] * SAMPLE_TEXTURE2D(_AxesTex, sampler_AxesTex, float2(k, 16)).r;
        decoded[5].b += coeffs[k] * SAMPLE_TEXTURE2D(_AxesTex, sampler_AxesTex, float2(k, 17)).r;

        decoded[6].r += coeffs[k] * SAMPLE_TEXTURE2D(_AxesTex, sampler_AxesTex, float2(k, 18)).r;
        decoded[6].g += coeffs[k] * SAMPLE_TEXTURE2D(_AxesTex, sampler_AxesTex, float2(k, 19)).r;
        decoded[6].b += coeffs[k] * SAMPLE_TEXTURE2D(_AxesTex, sampler_AxesTex, float2(k, 20)).r;

        decoded[7].r += coeffs[k] * SAMPLE_TEXTURE2D(_AxesTex, sampler_AxesTex, float2(k, 21)).r;
        decoded[7].g += coeffs[k] * SAMPLE_TEXTURE2D(_AxesTex, sampler_AxesTex, float2(k, 22)).r;
        decoded[7].b += coeffs[k] * SAMPLE_TEXTURE2D(_AxesTex, sampler_AxesTex, float2(k, 23)).r;

        decoded[8].r += coeffs[k] * SAMPLE_TEXTURE2D(_AxesTex, sampler_AxesTex, float2(k, 24)).r;
        decoded[8].g += coeffs[k] * SAMPLE_TEXTURE2D(_AxesTex, sampler_AxesTex, float2(k, 25)).r;
        decoded[8].b += coeffs[k] * SAMPLE_TEXTURE2D(_AxesTex, sampler_AxesTex, float2(k, 26)).r;
    }
    
    // --- Sample SH with sampleDir ---
    return EvalSH9(sampleDir, decoded);
}


#endif
